package com.yhn.hxds.dr.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhn.hxds.common.exception.BusinessException;
import com.yhn.hxds.common.utils.MicroAppUtil;
import com.yhn.hxds.dr.domain.DriverDO;
import com.yhn.hxds.dr.domain.DriverSettingsDO;
import com.yhn.hxds.dr.domain.WalletDO;
import com.yhn.hxds.dr.dto.req.RegisterNewDriverReqDTO;
import com.yhn.hxds.dr.mapper.DriverMapper;
import com.yhn.hxds.dr.mapper.DriverSettingsMapper;
import com.yhn.hxds.dr.mapper.WalletMapper;
import com.yhn.hxds.dr.service.DriverService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 86177
 * @description 针对表【tb_driver(代驾司机表)】的数据库操作Service实现
 * @createDate 2024-09-16 09:11:40
 */
@Service
public class DriverServiceImpl extends ServiceImpl<DriverMapper, DriverDO>
        implements DriverService {

    @Resource
    private MicroAppUtil microAppUtil;

    @Resource
    private DriverSettingsMapper settingsDao;

    @Resource
    private WalletMapper walletDao;
    @Override
    @Transactional
    public String registerNewDriver(RegisterNewDriverReqDTO requestParam) {
//        String openId = microAppUtil.getOpenId(requestParam.getCode());
        //判断注册的司机是否已经存在
        LambdaQueryWrapper<DriverDO> queryWrapper =  Wrappers.lambdaQuery(DriverDO.class)
                .eq(DriverDO::getOpenId, requestParam.getCode());
        Long count = baseMapper.selectCount(queryWrapper);
        //如果大于0，则抛出异常
        if (count > 0) {
            throw new BusinessException("该微信无法注册");
        }
        //转为map
        Map<String, Object> param = MapUtil.builder(new HashMap<String, Object>())
                .put("openId", requestParam.getCode())
                .put("nickname", requestParam.getNickname())
                .put("photo", requestParam.getPhoto())
                .build();
        //插入司机记录
        baseMapper.registerNewDriver(param);
        //查询司机的主键值
        LambdaQueryWrapper<DriverDO> driverWrapper =  Wrappers.lambdaQuery(DriverDO.class)
                .eq(DriverDO::getOpenId, requestParam.getCode())
                .select(DriverDO::getId);
        DriverDO driverDO = baseMapper.selectOne(driverWrapper);
        // 如果找到了对应的记录，返回其 id 字段的字符串形式
        String id = driverDO.getId().toString();
        //添加司机设置记录
        DriverSettingsDO settingsEntity = new DriverSettingsDO();
        settingsEntity.setDriverId(Long.parseLong(id));
        JSONObject json = new JSONObject();
        json.set("orientation", "");
        json.set("listenService", true);
        json.set("orderDistance", 0);
        json.set("rangeDistance", 5);
        json.set("autoAccept", false);
        settingsEntity.setSettings(json.toString());
        settingsDao.insert(settingsEntity);
        //添加司机钱包记录
        //支付密码为空，用户支付的时候，系统会自动提示用户设置支付密码
        WalletDO walletEntity = new WalletDO()
                .setDriverId(Long.parseLong(id))
                .setPassword(null)
                .setBalance(new BigDecimal("0"));
        walletDao.insert(walletEntity);
        return id;
    }
}







